/*
* Copyright (C) 2012 Manuel Pérez
*
* This file is part of OpenSudoku.
*
* OpenSudoku is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenSudoku is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenSudoku. If not, see <http://www.gnu.org/licenses/>.
*
*/
package igraf.Kotxeak;

import igraf.ItzuliInterfazea;
import igraf.Panela;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.sql.SQLException;
import java.util.List;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import modeloa.Koloreak;
import modeloa.Modeloa;
import dBasea.Kotxea;

public class KotxeBerria extends Panela {

	private static final long serialVersionUID = -8916691630953731116L;
	private JComponent zenbakia;
	private JTextField nif;
	private JButton ok;
	private JButton ezeztatu;
	private JLabel matri;
	private JLabel urtea;
	private JComboBox kolore, modeloa, markak;
	private JLabel marka;
	private Kotxea kotx=new Kotxea();

	public KotxeBerria(String tituloa, final ItzuliInterfazea itzuliInterfazea) {
		super(tituloa);

		JPanel erdikoPanela = this.getErdikoPanela();

		erdikoPanela.setBackground(SystemColor.activeCaption);
		erdikoPanela.setLayout(new BorderLayout());

		JPanel erdikoa = new JPanel();
		erdikoa.setBackground(SystemColor.activeCaption);
		erdikoPanela.add(erdikoa, BorderLayout.CENTER);
		erdikoa.setLayout(new GridLayout(7, 1));

		JPanel g = new JPanel();
		g.setBackground(SystemColor.activeCaption);
		erdikoa.add(g);

		JPanel geg = new JPanel();
		geg.setBackground(SystemColor.activeCaption);
		erdikoa.add(geg);
		JPanel arri = new JPanel();
		arri.setBackground(SystemColor.activeCaption);
		FlowLayout fl_arri = new FlowLayout();
		fl_arri.setHgap(15);
		fl_arri.setVgap(10);
		arri.setLayout(fl_arri);
		zenbakia = new JLabel("Sartu matrikula:");
		zenbakia.setFont(new Font("Arial", Font.PLAIN, 14));
		nif = new JTextField(8);
		nif.addKeyListener(new KeyAdapter() {// limitea NAN

			public void keyTyped(KeyEvent arg0) {
				if (nif.getText().length() >= 8) {
					arg0.consume();
				}
			}
		});
		arri.add(zenbakia);
		arri.add(nif);
		erdikoa.add(arri);

		JPanel arribb = new JPanel();
		arribb.setBackground(SystemColor.activeCaption);
		arri.setBackground(SystemColor.activeCaption);
		FlowLayout arrib = new FlowLayout();
		arrib.setHgap(15);
		arrib.setVgap(10);
		arribb.setLayout(arrib);

		matri = new JLabel("Kolorea");
		arribb.add(matri);
		kolore = new JComboBox();
		try {
			BoxCarga();
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null,
					"Ezin izan da kargatu DB-tik kolore zerrenda", "Oker",
					JOptionPane.ERROR_MESSAGE);

			e.printStackTrace();
		}
		matri.setFont(new Font("Arial", Font.PLAIN, 14));
		arribb.add(kolore);
		erdikoa.add(arribb);

		JPanel arr = new JPanel();
		arribb.setBackground(SystemColor.activeCaption);
		arr.setBackground(SystemColor.activeCaption);
		FlowLayout rib = new FlowLayout();
		rib.setHgap(15);
		rib.setVgap(10);
		arr.setLayout(rib);
		markak = new JComboBox();
		modeloa = new JComboBox();

		try {
			markakKarga();
			markak.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					try {
						BoxCargaa();
					} catch (SQLException e) {

						e.printStackTrace();
					}

				}
			});

		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null,
					"Ezin izan da kargatu DB-tik modelo zerrenda", "Oker",
					JOptionPane.ERROR_MESSAGE);

			e.printStackTrace();
		}
		try {
			BoxCargaa();
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null,
					"Ezin izan da kargatu DB-tik modelo zerrenda", "Oker",
					JOptionPane.ERROR_MESSAGE);

			e.printStackTrace();
		}
		marka = new JLabel("Marka");
		arr.add(marka);
		marka.setFont(new Font("Arial", Font.PLAIN, 14));
		arr.add(marka);
		arr.add(markak);
		urtea = new JLabel("Modeloa");
		arr.add(urtea);
		urtea.setFont(new Font("Arial", Font.PLAIN, 14));
		arr.add(modeloa);

		erdikoa.add(arr);

		// beheko panela
		JPanel behekoa = new JPanel();
		FlowLayout flowLayout = (FlowLayout) behekoa.getLayout();
		flowLayout.setHgap(110);
		flowLayout.setVgap(10);
		behekoa.setBackground(SystemColor.activeCaption);
		ok = new JButton("Kotxea sortu");
		ok.setHorizontalAlignment(SwingConstants.LEFT);
		ok.setFont(new Font("Arial", Font.PLAIN, 14));
		behekoa.add(ok);
		ezeztatu = new JButton("Ezeztatu");
		ezeztatu.setHorizontalAlignment(SwingConstants.RIGHT);
		ezeztatu.setFont(new Font("Arial", Font.PLAIN, 14));
		behekoa.add(ezeztatu);
		erdikoPanela.add(behekoa, BorderLayout.SOUTH);

		ok.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					if (!zerbaiIdatzi()) {
						JOptionPane.showMessageDialog(null,
								"Sartu behar duzu zerbait zuriunetan\n"
										+ "Ezin da eremu hutsik egon", "Oker",
								JOptionPane.ERROR_MESSAGE);
					} else if (kotx.existitzenDaMatrikula(nif.getText())) {
						JOptionPane.showMessageDialog(null,
								"Kotxe hau existitzen da\n"
										+ "Sartu matrikula berria duen kotxea",
								"Oker", JOptionPane.ERROR_MESSAGE);
					}

					else {
						try {
							Object ko = kolore.getSelectedItem();
							String koo = ko.toString();
							Object mo = modeloa.getSelectedItem();
							String moo = mo.toString();
							kotx.kotxeSortu(nif.getText(), koo, moo);
							JOptionPane.showMessageDialog(null,
									"Ondo sortu da kotxea", "",
									JOptionPane.INFORMATION_MESSAGE);
							eremuakGarbitu();
						} catch (NumberFormatException nfe) {
							JOptionPane.showMessageDialog(null, "Ezin izan da",
									"Oker", JOptionPane.ERROR_MESSAGE);
						}
					}

				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});

		ezeztatu.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				itzuliInterfazea.itzuliLehenPanela();
			}
		});
	}

	public void BoxCarga() throws SQLException {

		Vector<Koloreak> kolo = kotx.getKoloreak();

		for (int i = 0; i < kolo.size(); i = i + 1) {
			Koloreak Item = new Koloreak(kolo.get(i).getCodigo(), kolo.get(i)
					.getKoloreak());

			kolore.addItem(Item);
		}
	}

	public void markakKarga() throws SQLException {
		List<String> lista = kotx.getMarkak();
		markak.removeAllItems();
		if (lista != null) {
			for (String markas : lista) {
				markak.addItem(markas);
			}
		}
	}

	public void BoxCargaa() throws SQLException {
		Object marka = markak.getSelectedItem();
		String mar = marka.toString();
		Vector<Modeloa> kolo = kotx.getModeloa(mar);
		if (kolo != null) {
			modeloa.removeAllItems();
			for (int i = 0; i < kolo.size(); i = i + 1) {
				Modeloa Item = new Modeloa(kolo.get(i).getCodigo(), kolo.get(i)
						.getmodeloa(), kolo.get(i).getMarka());

				modeloa.addItem(Item);
			}
		}
	}

	private boolean zerbaiIdatzi() {
		boolean aurki;
		aurki = nif.getText().length() > 0;
		return aurki;
	}

	public void eremuakGarbitu() {
		nif.requestFocus();
		nif.setEnabled(true);
		nif.setText("");
		markak.setSelectedIndex(0);
		modeloa.setSelectedIndex(0);
		kolore.setSelectedIndex(0);

	}

}
